package nc.so.rest.pub.rule;

import java.util.*;

import nc.itf.scmpub.reference.uap.org.OrgUnitPubService;

import nc.vo.pubapp.pattern.pub.PubAppTool;
import nc.vo.so.m30.entity.SaleOrderBVO;
import nc.vo.so.m30.entity.SaleOrderHVO;
import nc.vo.so.pub.SOItemKey;
import nc.vo.so.pub.keyvalue.IKeyValue;

public class SOCurrencyRule {
  private SaleOrderHVO parentVO;
  private List<SaleOrderBVO> itemVOS;


  public SOCurrencyRule(SaleOrderHVO parentVO, List<SaleOrderBVO> itemVOS)
  {
    this.parentVO = parentVO;
    this.itemVOS = itemVOS;
  }
  public void setCurrency( ) {
    Set<String> settleorgs = new HashSet<String>();
    for (SaleOrderBVO itemVO : this.itemVOS) {
      String csettleorgid =itemVO.getCsettleorgid();
         // this.keyValue.getBodyStringValue(row, SOItemKey.CSETTLEORGID);
      if (!PubAppTool.isNull(csettleorgid)) {
        settleorgs.add(csettleorgid);
      }
    }

    Map<String, String> orgCurrMap = null;
    if (settleorgs.size() > 0) {
      String[] setorgs = new String[settleorgs.size()];
      settleorgs.toArray(setorgs);
      orgCurrMap = this.getOrgCurrByPks(setorgs);
    }
    if (null == orgCurrMap) {
      orgCurrMap = new HashMap<String, String>();
    }
    for (SaleOrderBVO itemVO : this.itemVOS) {
      String csettleorgid =itemVO.getCsettleorgid();
          //this.keyValue.getBodyStringValue(row, SOItemKey.CSETTLEORGID);
      if (PubAppTool.isNull(csettleorgid)) {
        itemVO.setCcurrencyid(null);
        //this.keyValue.setBodyValue(row, SOItemKey.CCURRENCYID, null);
      }
      else {
        itemVO.setCcurrencyid(orgCurrMap.get(csettleorgid));
//        this.keyValue.setBodyValue(row, SOItemKey.CCURRENCYID,
//            orgCurrMap.get(csettleorgid));
      }
    }
  }

  private Map<String, String> getOrgCurrByPks(String[] pks) {
    Map<String, String> orgCurrMap = null;
    orgCurrMap = OrgUnitPubService.queryOrgCurrByPk(pks);
    return orgCurrMap;
  }
}
